package com.xiyu.service.repository.unit.unit;

import com.xiyu.service.model.unit.unit.UnitUnitTable;
import com.xiyu.service.vo.unit.unit.UnitPageQueryInput;
import com.xiyu.service.model.unit.unit.UnitUnit;
import org.babyfish.jimmer.spring.repository.JRepository;
import org.babyfish.jimmer.sql.ast.tuple.Tuple2;
import org.babyfish.jimmer.Page;

import java.util.List;

public interface UnitUnitRepository extends JRepository<UnitUnit, Long> {
    UnitUnitTable unitUnitTable = UnitUnitTable.$;

    default Page<UnitUnit> pageQuery(UnitPageQueryInput inputVO){
        return
            sql().createQuery(unitUnitTable)
                    .whereIf(inputVO.getInstitutionalSequence() != null, unitUnitTable.institutionalSequence().eq(inputVO.getInstitutionalSequence()))
                    .where(unitUnitTable.name().likeIf(inputVO.getName()))
                    .orderBy(unitUnitTable.id().desc())
                    .select(unitUnitTable)
                    .fetchPage(inputVO.getPageNo() - 1, inputVO.getPageSize());

    }

    default List<Tuple2<Integer, Long>> findGroupCategory(){
        return sql().createQuery(unitUnitTable)
                .groupBy(unitUnitTable.institutionalCategory())
                .select(unitUnitTable.institutionalCategory(),
                        unitUnitTable.id().count())
                .execute();
    }


}